package com.app.demo.entity;

import com.app.core.encrypt.Encrypt;
import com.app.core.mvc.serialization.Dictionary;
import com.app.core.mvc.serialization.desensitization.annotation.CellphoneDesensitization;
import com.app.core.mybatis.BaseEntity;
import com.app.demo.dictionary.DictionaryEnum;
import com.app.demo.enums.UserType;
import com.app.logrecord.annotation.LogRecordField;
import com.app.logrecord.translator.EnumTranslator;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.apache.ibatis.type.JdbcType;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;

/**
 * 用户表
 *
 * @author qiangt
 * @since 2022-10-24
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
@ApiModel(description = "用户表")
@TableName(value = "user", autoResultMap = true)
public class User extends BaseEntity {

    @LogRecordField(value = "用户名")
    @NotEmpty(message = "用户名不能为空")
    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private Encrypt credentials;

    @ApiModelProperty(value = "昵称")
    private String nickname;

    @ApiModelProperty(value = "用户唯一标识")
    private Long uid;

    @LogRecordField(value = "手机号")
    @ApiModelProperty(value = "手机号")
    @CellphoneDesensitization
    private String cellphone;

    @ApiModelProperty(value = "邮箱")
    //    @EmailDesensitization
    private String email;

    @ApiModelProperty(value = "用户头像")
    private String avatar;

    @ApiModelProperty(value = "性别")
    private String sex;

    @Max(100)
    @Min(1)
    @LogRecordField(value = "年龄")
    @ApiModelProperty(value = "年龄")
    private Integer age;

    /**
     * 字段类型处理器：
     * 注意！！ 必须开启映射注解 @TableName(autoResultMap = true)
     * 以下两种类型处理器，二选一 也可以同时存在
     * 注意！！选择对应的 JSON 处理器也必须存在对应 JSON 解析依赖包
     */
    // @TableField(typeHandler = JacksonTypeHandler.class)
    // @TableField(typeHandler = FastjsonTypeHandler.class)
    @ApiModelProperty(value = "角色ID集合，Long数组", example = "[1,2,3]", position = 12)
    @TableField(jdbcType = JdbcType.JAVA_OBJECT)
    //    @LongArrayEncryptJsonConverter
    private Long[] roles;

    @LogRecordField(value = "用户类型", translator = EnumTranslator.class)
    @ApiModelProperty(value = "用户类型", position = 14)
    private UserType type;

    @LogRecordField(value = "用户来源")
    @Dictionary(DictionaryEnum.Names.UserSource)
    @ApiModelProperty(value = "用户来源", position = 15)
    private String source;

    @ApiModelProperty(name = "enabled", value = "状态")
    private Boolean enabled;

    @ApiModelProperty(value = "密码更新日期", position = 16)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date passwordUpdateDate;

    @ApiModelProperty(value = "是否初始密码", position = 17)
    private Boolean passwordMustUpdate;

    @TableField(exist = false)
    private List<String> roleList;

}
